package com.situ.stl.controller;

import com.situ.stl.constants.Constants;

import com.situ.stl.entity.Reserve;
import com.situ.stl.service.ReserveService;
import com.situ.stl.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/reserve")
@Slf4j
public class ReserveController {
    @Autowired
    private ReserveService reserveService;
    @GetMapping
    public Result getAll(Integer page, Integer limit, Reserve reserve) {
        if (ObjectUtils.isEmpty(page) && ObjectUtils.isEmpty(limit)) {
            List<Reserve> reserveList = reserveService.select(reserve);
            return Result.success(Constants.STATUS_200, reserveList);
        }
        return Result.success(Constants.STATUS_200, reserveService.searchByPage(page, limit, reserve));
    }
    @PostMapping
    public Result add(@RequestBody Reserve reserve) {

        try {
            int row = reserveService.addReserve(reserve);
            if (row == 0) {
                return Result.error(Constants.EDIT_RESERVE_ERROR, "添加预约失败");
            }
        } catch (Exception e) {
            log.info("添加失败");
            return Result.success(Constants.STATUS_10001, e.getMessage());
        }
        return Result.success(Constants.STATUS_200);

    }
    @PutMapping
    public Result modifyReserve(@RequestBody Reserve reserve) {
        try {
            int row = reserveService.modifyReserve(reserve);
            if (row == 0) {
                return Result.error(Constants.EDIT_RESERVE_ERROR, "修改预约失败");
            }

        } catch (Exception e) {
            log.info("修改失败");
            return Result.success(Constants.STATUS_10001, e.getMessage());
        }
        return Result.success(Constants.STATUS_200);
    }
    @PutMapping("/qd")
    public Result modifyReserve1(@RequestBody Reserve reserve) {
        try {
            int row = reserveService.modifyReserve4(reserve);
            if (row == 0) {
                return Result.error(Constants.EDIT_RESERVE_ERROR, "修改预约失败");
            }else if(row == -1){
                return Result.error(Constants.EDIT_RESERVE_ERROR, "该学员已未到，无需重复");
            }

        } catch (Exception e) {
            log.info("修改失败");
            return Result.error(Constants.STATUS_10001, e.getMessage());
        }
        return Result.success(Constants.STATUS_200);
    }
    // 在ReserveController中添加专门的取消接口
    @PutMapping("/cancel/{id}")
    public Result cancelReserve(@PathVariable Integer id) {
        try {

            Reserve reserve = new Reserve();
            reserve.setId(id);
            reserve.setStatus(0); // 设置为已取消状态
            int row = reserveService.modifyReserve2(reserve);

            if (row == 0) {
                return Result.error(Constants.EDIT_RESERVE_ERROR, "取消预约失败");
            }
        } catch (Exception e) {
            log.info("取消失败");
            return Result.success(Constants.STATUS_10001, e.getMessage());
        }
        return Result.success(Constants.STATUS_200);
    }
    @PutMapping("/batchSign")
    public Result batchSign(@RequestBody List<Reserve> reserves){
        try {
            reserveService.batchReserve(reserves);
            return Result.success(Constants.STATUS_200);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constants.STATUS_10001,e.getMessage());
        }
    }


}
